﻿using System;
using System.ComponentModel;
using System.Globalization;

namespace FBProject.BusinessLogic
{
    [TypeConverter(typeof(TaskDifferenceTypeConverter))]
    public enum TaskDifferenceType
    {
        Name,
        Estimate,
        RemainingWork,
        Resource,
        Priority,
        Milestone, 
        Completed,
        NewTask,
        DeletedTask
    }

    public class TaskDifferenceTypeConverter : TypeConverter
    {
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
                switch ((TaskDifferenceType)value)
                {
                    case TaskDifferenceType.RemainingWork:
                        return "Remaining Work";
                    case TaskDifferenceType.NewTask:
                        return "New Task";
                    case TaskDifferenceType.DeletedTask:
                        return "Deleted Task";
                }
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }
    }

    public class TaskDifference
    {
        public int TaskId { get; set; }

        public string FBCaseDescription { get; set; }

        public string FBCaseUrl { get; set; }

        public string MSProjectValue { get; set; }

        public string ExternalValue { get; set; }

        public TaskDifferenceType DifferenceType { get; set; }

        public TaskDifference(int taskId, string fbCaseDescription, string fbCaseUrl, TaskDifferenceType differenceType)
            : this(taskId, fbCaseDescription, fbCaseUrl, differenceType, null, null)
        { }

        public TaskDifference(int taskId, string fbCaseDescription, string fbCaseUrl, TaskDifferenceType differenceType, string msProjectValue, string externalValue)
        {
            this.TaskId = taskId;
            this.FBCaseDescription = fbCaseDescription;
            this.FBCaseUrl = fbCaseUrl;
            this.DifferenceType = differenceType;
            this.ExternalValue = externalValue;
            this.MSProjectValue = msProjectValue;
        }

        public string MSProjectColumnName
        {
            get
            {
                string msProjectColumnName;
                switch (this.DifferenceType)
                {
                    case TaskDifferenceType.Milestone:
                        msProjectColumnName = "Text2";
                        break;
                    case TaskDifferenceType.Name:
                        msProjectColumnName = "Name";
                        break;
                    case TaskDifferenceType.Priority:
                        msProjectColumnName = "Priority";
                        break;
                    case TaskDifferenceType.Estimate:
                        msProjectColumnName = "Number1";
                        break;
                    case TaskDifferenceType.RemainingWork:
                        msProjectColumnName = "Remaining Work";
                        break;
                    case TaskDifferenceType.Resource:
                        msProjectColumnName = "Resource Names";
                        break;
                    case TaskDifferenceType.NewTask:
                    case TaskDifferenceType.DeletedTask:
                    case TaskDifferenceType.Completed:
                        msProjectColumnName = "Text1";
                        break;
                    default:
                        throw new InvalidOperationException(string.Format("Unknown Task Difference Type: {0}", this.DifferenceType.ToString()));
                }
                return msProjectColumnName;
            }
        }

        public override string ToString()
        {
            return string.Format("{0} {1} {2} {3} {4}",
                this.DifferenceType,
                this.FBCaseDescription,
                this.TaskId,
                this.ExternalValue,
                this.MSProjectValue);
        }

        public override int GetHashCode()
        {
            return this.ToString().GetHashCode();
        }

        public override bool Equals(object obj)
        {
            var taskDifference = obj as TaskDifference;
            if (taskDifference == null)
                return false;

            return string.CompareOrdinal(taskDifference.ToString(), this.ToString()) == 0;
        }
    }
}
